package com.neu.controller;


import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.neu.common.Result;

import com.neu.entity.Audit;
import com.neu.entity.Hire;

import com.neu.entity.Params;

import com.neu.service.HireService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/hire")
public class HireController {
    @Autowired
    private HireService hireService;
//    @Autowired
//    private HotelService hotelService;
//    @Autowired
//    private HotelDao hotelDao;

    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Hire> info = hireService.findBySearch(params);
        return Result.success(info);
    }

    @PostMapping
    public Result add(@RequestBody Hire hire){
//        if(reserve.getId() == null){
//            reserveService.add(reserve);
//        }else{
//            reserveService.update(reserve);
//        }
        //TODO
        //1.酒店剩余房间是否为0，大于0的时候才可以被预定
//        Hotel hotel = hotelDao.selectByPrimaryKey(reserve.getHotelId());
//        if(hotel.getNum() == 0){
//            return Result.error("酒店该房间已预订完，请预订其他房间");
//        }

        //2.往预订表里插入一条预订记录
        hire.setTime(DateUtil.now());
        hire.setPay("未支付");
        hireService.add(hire);
        //3.对应的酒店房间剩余数量减一
//        hotel.setNum(hotel.getNum()-1);
//        hotelDao.updateByPrimaryKeySelective(hotel);

        return Result.success();
    }

    @PostMapping("/audit")
    public Result audit(@RequestBody Hire hire){
        hireService.audit(hire);
        return Result.success();
    }

    @PutMapping("/payment")
    public Result payment(@RequestBody Hire hire){
        hireService.payment(hire);
        return Result.success();
    }

//    @PutMapping("/evaluate")
//    public Result evaluate(@RequestBody Hire hire){
//        hireService.evaluate(hire);
//        return Result.success();
//    }

    @PutMapping("/interact")
    public Result interact(@RequestBody Hire hire){
        hireService.interact(hire);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        hireService.delete(id);
        return Result.success();
    }
}
